package zlx.springcloud.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import zlx.commons.entities.CommonResult;
import zlx.commons.entities.Payment;
import zlx.springcloud.service.PaymentService;


import javax.annotation.Resource;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@RestController
@Slf4j
public class PaymentController {
    @Resource
    PaymentService paymentService;

    @Value("${server.port}")
    private String serverPort;

    @PostMapping(value = "/payment/create")
    public CommonResult create(@RequestBody Payment payment){
        int result =paymentService.create(payment);
        log.info("*****插入结果"+result);

         if(result>0){
             return new CommonResult(200,"插入数据成功,端口号是"+serverPort,result);
         }else
             return new CommonResult(444,"插入数据失败",null);
    }
    @GetMapping(value = "/payment/get/{id}")
    public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id){
        Payment payment = paymentService.getPaymentById(id);
        log.info("*****查询结果"+payment);

        if(payment!=null){
            return new CommonResult(200,"查询数据成功,端口号是"+serverPort,payment);
        }else
            return new CommonResult(444,"没有对应的数据，查询ID："+id,null);
    }
    @RequestMapping (value = "/payment/lb")
    public String paymentzk(){
        log.info("springcloud with lb:"+serverPort+"\t"+ UUID.randomUUID().toString());
        return "springcloud with lb:"+serverPort+"\t"+ UUID.randomUUID().toString();
    }
    @GetMapping(value = "/payment/feign/timeout")
    public String paymentTimeout(){
        try {
            TimeUnit.SECONDS.sleep(3);
        }catch (InterruptedException e){
            e.printStackTrace();
        }
        return serverPort;
    }
}
